import requests
from bs4 import BeautifulSoup
import os
import time

def fetch_nested_formula_elements(url, parent_class="item", target_class="formula", target2_class="item-info"):
    """
    获取网页中父标签class="item"下的子标签class="formula"的HTML信息

    Args:
        url: 网页URL
        parent_class: 父标签的class名称
        target_class: 目标子标签的class名称
        target2_class: 目标子标签2的class名称

    Returns:
        list: 包含标签HTML内容的列表
    """
    try:
        # 设置请求头，模拟浏览器访问
        headers = {
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'
        }

        print(f"正在访问网页: {url}")
        response = requests.get(url, headers=headers, timeout=10)
        response.raise_for_status()  # 如果请求失败则抛出异常

        # 检查编码并设置
        if response.encoding == 'ISO-8859-1':
            response.encoding = response.apparent_encoding

        print("网页访问成功，开始解析...")

        # 使用BeautifulSoup解析HTML
        soup = BeautifulSoup(response.text, 'html.parser')

        # 首先查找所有class为"item"的父标签
        parent_elements = soup.find_all(class_=parent_class)
        print(f"找到 {len(parent_elements)} 个 class='{parent_class}' 的父标签")

        # 在每个父标签中查找class为"formula"的子标签
        formula_html_list = []
        parent_count = 0

        for parent_idx, parent_element in enumerate(parent_elements, 1):
            # 在父标签内查找formula子标签
            formula_elements = parent_element.find_all(class_=target_class)

            if formula_elements:
                parent_count += 1
                print(f"在父标签 #{parent_idx} 中找到 {len(formula_elements)} 个 class='{target_class}' 的子标签")

            item_info_elements = parent_element.find_all(class_=target2_class)
            if item_info_elements:
                print(f"在父标签 #{parent_idx} 中找到 {len(item_info_elements)} 个 class='{target2_class}' 的子标签")

            for formula_idx, formula_element in enumerate(formula_elements, 1):
                formula_html = {
                    'parent_index': parent_idx,
                    'formula_index': formula_idx,
                    'parent_html': str(parent_element)[:200] + "..." if len(str(parent_element)) > 200 else str(
                        parent_element),
                    'html': str(formula_element),
                    'text': formula_element.get_text(strip=True),
                    'full_context': f"位于第{parent_idx}个{parent_class}标签内的第{formula_idx}个{target_class}标签",
                    "type": "merge"
                }
                formula_html_list.append(formula_html)
            for item_info_idx, item_info_element in enumerate(item_info_elements, 1):
                item_info_html = {
                    'parent_index': parent_idx,
                    'formula_index': item_info_idx,
                    'parent_html': str(parent_element)[:200] + "..." if len(str(parent_element)) > 200 else str(
                        parent_element),
                    'html': str(item_info_element),
                    'text': item_info_element.get_text(strip=True),
                    'full_context': f"位于第{parent_idx}个{parent_class}标签内的第{item_info_idx}个{target_class}标签",
                    "type": "origin"
                }
                formula_html_list.append(item_info_html)

        print(f"在 {parent_count} 个父标签中共找到 {len(formula_html_list)} 个符合条件的 {target_class} 标签")

        return formula_html_list

    except requests.exceptions.RequestException as e:
        print(f"网络请求错误: {e}")
        return []
    except Exception as e:
        print(f"解析过程中发生错误: {e}")
        return []


def save_to_file(formula_html_list, filename="nested_formula_elements.html", parent_class="item",
                 target_class="formula"):
    """
    将提取的嵌套HTML内容保存到文件

    Args:
        formula_html_list: 包含HTML内容的列表
        filename: 保存的文件名
        parent_class: 父标签class名称
        target_class: 目标标签class名称
    """
    try:
        with open(filename, 'w', encoding='utf-8') as file:
            # 写入文件头部
            file.write('<!DOCTYPE html>\n<html>\n<head>\n<meta charset="UTF-8">\n')
            file.write('<title>提取的嵌套Formula元素</title>\n')
            file.write('<style>\n')
            file.write('body { font-family: Arial, sans-serif; margin: 20px; }\n')
            file.write('.formula-item { border: 1px solid #ccc; margin: 10px 0; padding: 15px; }\n')
            file.write('.original-html { background: #f5f5f5; padding: 10px; margin: 10px 0; }\n')
            file.write('.text-content { background: #e8f4fd; padding: 10px; margin: 10px 0; }\n')
            file.write('.context-info { background: #fff3cd; padding: 8px; margin: 5px 0; }\n')
            file.write('</style>\n</head>\n<body>\n')
            file.write(f'<h1>从网页提取的嵌套 {target_class} 元素</h1>\n')
            file.write(f'<p>查找条件: {parent_class} → {target_class}</p>\n')
            file.write(f'<p>共找到 {len(formula_html_list)} 个符合条件的元素</p>\n<hr>\n')

            # 按父标签分组显示
            current_parent = 0
            for item in formula_html_list:
                if item['parent_index'] != current_parent:
                    current_parent = item['parent_index']
                    file.write(f'<h2>父标签 #{current_parent} (class="{parent_class}")</h2>\n')

                file.write(f'<div class="formula-item">\n')
                file.write(f'<div class="context-info">\n')
                file.write(f'<strong>位置:</strong> {item["full_context"]}\n')
                file.write('</div>\n')
                file.write(f'<h3>子标签 #{item["formula_index"]} (class="{target_class}")</h3>\n')
                file.write(f'<div class="original-html">\n')
                file.write('<strong>完整HTML:</strong><br>\n')
                file.write(item['html'] + '\n')
                file.write('</div>\n')
                file.write(f'<div class="text-content">\n')
                file.write(f'<strong>文本内容:</strong> {item["text"]}\n')
                file.write('</div>\n')
                file.write('</div>\n\n')

            # 写入文件尾部
            file.write('</body>\n</html>')

        print(f"内容已保存到文件: {filename}")
        return True

    except Exception as e:
        print(f"保存文件时发生错误: {e}")
        return False


def save_as_raw_html(formula_html_list, filename="nested_formula_elements_raw.html"):
    """
    保存原始HTML内容（不添加额外格式）
    """
    try:
        with open(filename, 'w', encoding='utf-8') as file:
            file.write('<!-- 提取的嵌套 formula 元素原始HTML -->\n')
            file.write('<!-- 查找条件: item → formula -->\n\n')

            current_parent = 0
            for item in formula_html_list:
                if item['parent_index'] != current_parent:
                    current_parent = item['parent_index']
                    file.write(f'\n<!-- ===== 父标签 #{current_parent} ===== -->\n')

                file.write(f'<!-- 子标签 #{item["formula_index"]} -->\n')
                file.write(f'<!-- 上下文: {item["full_context"]} -->\n')
                file.write(item['html'] + '\n')
                file.write('<!-- --------------------------------- -->\n\n')

        print(f"原始HTML已保存到文件: {filename}")
        return True
    except Exception as e:
        print(f"保存原始HTML时发生错误: {e}")
        return False


def analyze_structure(formula_html_list):
    """
    分析提取结果的结构
    """
    if not formula_html_list:
        print("没有找到任何元素，无法分析结构")
        return

    print("\n=== 结构分析 ===")

    # 统计每个父标签中的子标签数量
    parent_stats = {}
    for item in formula_html_list:
        parent_idx = item['parent_index']
        if parent_idx not in parent_stats:
            parent_stats[parent_idx] = 0
        parent_stats[parent_idx] += 1

    print("各父标签中的子标签分布:")
    for parent_idx, count in sorted(parent_stats.items()):
        print(f"  父标签 #{parent_idx}: {count} 个子标签")

    # 文本长度分析
    text_lengths = [len(item['text']) for item in formula_html_list]
    if text_lengths:
        avg_length = sum(text_lengths) / len(text_lengths)
        print(f"子标签文本平均长度: {avg_length:.1f} 字符")
        print(f"最短文本: {min(text_lengths)} 字符")
        print(f"最长文本: {max(text_lengths)} 字符")


def main():
    """主函数"""
    # 目标URL
    url = "https://wiki.biligame.com/dsp/%E5%BC%95%E5%8A%9B%E7%9F%A9%E9%98%B5"

    # 获取嵌套的formula元素
    formula_elements = fetch_nested_formula_elements(
        url,
        parent_class="item",
        target_class="formula"
    )

    if not formula_elements:
        print("未找到任何符合条件的元素，程序结束")
        return

    # 显示找到的内容摘要
    print("\n找到的内容摘要:")
    for item in formula_elements[:5]:  # 只显示前5个作为示例
        print(f"{item['full_context']}:")
        print(f"  文本预览: {item['text'][:100]}...")
        print()

    if len(formula_elements) > 5:
        print(f"... 还有 {len(formula_elements) - 5} 个元素未显示")

    # 分析结构
    analyze_structure(formula_elements)

    # 保存到格式化的HTML文件
    save_to_file(
        formula_elements,
        "output/nested_formula_elements_formatted.html",
        parent_class="item",
        target_class="formula"
    )

    # 保存原始HTML
    save_as_raw_html(formula_elements, "output/nested_formula_elements_raw.html")

    # 同时保存为文本文件，方便查看
    try:
        with open("output/nested_formula_elements_summary.txt", 'w', encoding='utf-8') as file:
            file.write(f"从 {url} 提取的嵌套 formula 元素\n")
            file.write(f"查找条件: item → formula\n")
            file.write(f"提取时间: {time.strftime('%Y-%m-%d %H:%M:%S')}\n")
            file.write(f"共找到 {len(formula_elements)} 个符合条件的元素\n\n")

            current_parent = 0
            for item in formula_elements:
                if item['parent_index'] != current_parent:
                    current_parent = item['parent_index']
                    file.write(f"\n{'=' * 60}\n")
                    file.write(f"父标签 #{current_parent} (class='item')\n")
                    file.write(f"{'=' * 60}\n\n")

                file.write(f"子标签 #{item['formula_index']}:\n")
                file.write(f"上下文: {item['full_context']}\n")
                file.write(f"文本内容: {item['text']}\n")
                file.write(f"HTML内容:\n{item['html']}\n")
                file.write("-" * 50 + "\n\n")

        print("摘要已保存到: nested_formula_elements_summary.txt")
    except Exception as e:
        print(f"保存摘要文件时发生错误: {e}")


if __name__ == "__main__":
    # 检查是否需要安装依赖
    # try:
    #     import requests
    #     from bs4 import BeautifulSoup
    # except ImportError:
    #     print("请先安装所需依赖:")
    #     print("pip install requests beautifulsoup4")
    #     exit(1)

    main()